package top.hidoctor.auscultation.feature.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.tang.base.BaseResponse;
import com.tang.base.NetMapEncryptUtils;
import com.tang.net.NetClient;
import com.tang.user.UserInfoManager;
import com.tang.util.GsonUtil;
import com.tang.util.SPUtil;
import com.tang.util.TimeUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.base.Constant;
import top.hidoctor.auscultation.bean.PatientBean;
import top.hidoctor.auscultation.bean.PatientLocalInfo;
import top.hidoctor.auscultation.event.XinyinEvent;
import top.hidoctor.auscultation.net.Api;
import top.hidoctor.auscultation.net.c.UpdateContract;
import top.hidoctor.auscultation.net.p.UpdatePresenter;
import top.hidoctor.auscultation.net.req.AddScreenReq;

public class XinyinPatientAct extends BaseActivity implements UpdateContract.View {

    private TextView tv_name, tv_gender, tv_brithday, tv_shengao, tv_tizhong, tv_address, tv_add_detail, tv_phone, tv_idcard;
    private Button btn_updata;
    private PatientLocalInfo info;
    private UpdatePresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_xinyin_patient);
        EventBus.getDefault().register(this);
        presenter = new UpdatePresenter(this, this);
        initView();
        initEvent();
    }


    private void initView() {
        final Toolbar toolbar = findViewById(R.id.toolbar);
        TextView tvTitle = findViewById(R.id.tv_title);
        TextView tvSetting = findViewById(R.id.tv_setting);
        toolbar.setNavigationIcon(R.mipmap.ic_back_left);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        tvTitle.setText("详情");
        tvSetting.setText("重新绑定");
        tv_name = findViewById(R.id.tv_name);
        tv_gender = findViewById(R.id.tv_gender);
        tv_brithday = findViewById(R.id.tv_brithday);
        tv_shengao = findViewById(R.id.tv_shengao);
        tv_tizhong = findViewById(R.id.tv_tizhong);
        tv_address = findViewById(R.id.tv_address);
        tv_add_detail = findViewById(R.id.tv_add_detail);
        tv_phone = findViewById(R.id.tv_phone);
        tv_idcard = findViewById(R.id.tv_idcard);
        btn_updata = findViewById(R.id.btn_updata);

        tvSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(XinyinPatientAct.this, PatientsActivity.class)
                        .putExtra("info", info));
            }
        });

        btn_updata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddScreenReq req = new AddScreenReq();
                req.setCalm(info.getZhengjing()); //,0否,1是
                req.setCollectionAddress(info.getAddress());
                req.setHospitalId(UserInfoManager.getInstance().getUserInfo().getHospitalId());
                req.setCoordinate(info.getPeiheId());
                req.setHeight(info.getShengao());
                req.setPatientId(info.getUserId());
                req.setOperation(info.getShoushu());
                req.setWeight(info.getTizhong());
                req.setSite(info.getChangsuoId());
                req.setPosition(info.getTiweiId());
                req.setNo(info.getFileName().split(".wav")[0]);
                req.setPatient(new AddScreenReq.PatientBean(info.getUserId()));
                presenter.update(req, info.getFileName(), info.getDuration());
            }
        });
    }

    private void initEvent() {
        info = (PatientLocalInfo) getIntent().getSerializableExtra("info");
        getDetail(info.getUserId());
    }

    private void getDetail(String id) {
        if (TextUtils.isEmpty(id)) {
            return;
        }
        showLoading();
        Map<String, Object> map = new NetMapEncryptUtils("").getMaps();
        NetClient.getInstance(Api.class)
                .patientDetail(id, map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Observer<BaseResponse<PatientBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<PatientBean> value) {
                        PatientBean data = value.getData();
                        if (data != null) {
                            tv_name.setText(data.getName());
                            tv_gender.setText(data.getGender() == 1 ? "男" : "女");
                            tv_brithday.setText(TimeUtil.formatTime(data.getBirthday(), TimeUtil.Y_M_D));
                            tv_shengao.setText(info.getShengao());
                            tv_tizhong.setText(info.getTizhong());
                            tv_address.setText(data.getAddress());
                            tv_add_detail.setText(data.getDetailAddress());
                            tv_phone.setText(data.getPhone());
                            tv_idcard.setText(data.getIdCard());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                    }

                    @Override
                    public void onComplete() {
                        hideLoading();
                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(XinyinEvent event) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    @Override
    public void updateSuc() {
        //删除数组
        String content = (String) SPUtil.get(this, Constant.LOCAL_INFO, "");
        if (!TextUtils.isEmpty(content)) {
            List<PatientLocalInfo> data = GsonUtil.parseJson(content, new TypeToken<List<PatientLocalInfo>>() {
            });
            for (int i = 0; i < data.size(); i++) {
                if (data.get(i).getFileName().equals(info.getFileName())) {
                    data.get(i).setUpload(true);
                    break;
                }
            }
            SPUtil.put(this, Constant.LOCAL_INFO, GsonUtil.toJsonStr(data));
        }

        finish();
        showToast("上传成功");
        EventBus.getDefault().post(new XinyinEvent());
    }
}
